@namespace MudBlazor
@typeparam T
@inherits MudBaseInput<T>

<CascadingValue Name="SubscribeToParentForm" Value="false" IsFixed="true">
    <div class="@OuterClassname" id="@_elementId" @onfocusout="@OnFocusOutAsync" @onclick:stopPropagation @onmousedown:stopPropagation>
        <MudInputControl Label="@Label"
                         Variant="@Variant"
                         HelperId="@GetHelperId()"
                         HelperText="@HelperText"
                         HelperTextOnFocus="@HelperTextOnFocus"
                         FullWidth="@FullWidth"
                         Margin="@Margin"
                         Class="@Classname"
                         Style="@Style"
                         Error="@HasErrors"
                         ErrorId="@ErrorId"
                         ErrorText="@GetErrorText()"
                         Disabled="@GetDisabledState()"
                         Required="@Required"
                         ForId="@InputElementId"
                         @onmousedown="@HandleMouseDown">
            <InputContent>
                <MudInput @ref="_elementReference"
                          InputType="@(CanRenderValue || (Strict && !IsValueInList) ? InputType.Hidden : InputType.Text)"
                          Class="@InputClassname"
                          Margin="@Margin"
                          Placeholder="@Placeholder"
                          Label="@Label"
                          Variant="@Variant"
                          Typo="@Typo"
                          TextUpdateSuppression="false"
                          Value="@(Strict && !IsValueInList ? null : Text)"
                          Underline="@Underline"
                          Disabled="@GetDisabledState()"
                          ReadOnly="true"
                          Error="@Error"
                          ErrorId="@ErrorId"
                          HelperId="@GetHelperId()"
                          HelperText="@HelperText"
                          AdornmentText="@AdornmentText"
                          AdornmentIcon="@_currentIcon"
                          Adornment="@Adornment"
                          AdornmentColor="@AdornmentColor"
                          IconSize="@IconSize"
                          AdornmentAriaLabel="@AdornmentAriaLabel"
                          OnAdornmentClick="@OnAdornmentClick"
                          Clearable="@(Clearable && !GetReadOnlyState())"
                          ClearIcon="@ClearIcon"
                          OnClearButtonClick="@(async (e) => await SelectClearButtonClickHandlerAsync(e))"
                          @attributes="UserAttributes"
                          OnBlur="@OnBlurAsync"
                          ShrinkLabel="@ShrinkLabel"
                          InputId="@InputElementId"
                          Required="@Required">
                    @if (CanRenderValue)
                    {
                        @GetSelectedValuePresenter()
                    }
                </MudInput>
                @if (FitContent)
                {
                    <MudStack Class="@FillerClassname" Spacing="1" Row>
                        @{
                            var value = _longestItem is { Value: not null } ? ToStringFunc ?.Invoke(_longestItem.Value) ?? Converter.Set(_longestItem.Value) : string.Empty;
                            var useLabel = Label is not null && Placeholder is null && !ShrinkLabel
                                        && (Adornment != Adornment.Start || (AdornmentIcon is null && AdornmentText is null));

                            var defaultValue = useLabel ? Label : Placeholder;
                        }
                        @if (defaultValue?.Length > value!.Length)
                        {
                            <MudText Typo="@Typo">@(MultiSelectionTextFunc?.Invoke([defaultValue]) ?? defaultValue)</MudText>
                        }
                        else if (_longestItem?.ChildContent is null || MultiSelectionTextFunc is not null)
                        {
                            <MudText Typo="@Typo">@(MultiSelectionTextFunc?.Invoke([value!]) ?? value!)</MudText>
                        }
                        else
                        {
                            <MudText Typo="@Typo">@_longestItem.ChildContent</MudText>
                        }
                        @AdornmentText
                        @if (AdornmentText is null)
                        {
                            <MudIcon Icon="@(AdornmentIcon ?? OpenIcon)" Size="IconSize"></MudIcon>
                        }
                        @if (Clearable)
                        {
                            <MudIcon Icon="@ClearIcon" Size="IconSize"></MudIcon>
                        }
                    </MudStack>
                }
                <MudPopover Open=@(_open)
                            MaxHeight="@MaxHeight"
                            Class="@PopoverClass"
                            AnchorOrigin="@AnchorOrigin"
                            TransformOrigin="@TransformOrigin"
                            Fixed="@DropdownSettings.Fixed"
                            OverflowBehavior="@DropdownSettings.OverflowBehavior"
                            RelativeWidth="@RelativeWidth">
                    <CascadingValue Value="@((IMudSelect)this)" IsFixed="true">
                        <MudList T="string"
                                 Class="@ListClass"
                                 Dense="@Dense"
                                 @bind-SelectedValue="_activeItemId">
                            @if (MultiSelection && SelectAll)
                            {
                                <MudListItem Value="@("__SelectAll__")"
                                             Icon="@SelectAllCheckBoxIcon"
                                             Text="@SelectAllText"
                                             OnClick="SelectAllClickAsync"
                                             OnClickPreventDefault="true"
                                             Dense="@Dense"
                                             Class="mb-2" />
                                <MudDivider />
                            }
                            @ChildContent
                        </MudList>
                    </CascadingValue>
                </MudPopover>
            </InputContent>
        </MudInputControl>
    </div>
    @*Shadow select items for IsValueInList and CanRenderValue*@
    <CascadingValue Value="@((IMudShadowSelect)this)" IsFixed="true">
        <CascadingValue Name="HideContent" Value="true">
            @ChildContent
        </CascadingValue>
    </CascadingValue>
</CascadingValue>
<!-- 
pointerdown instead of click needed to close the menu before OnLostFocus runs

When Modal is false the overlay does not receive pointer events so we activate
the auto close feature and call CloseMenu in OnClosed. The modeless auto close
feature relies on pointerdown event resulting in the same order of events.
-->
<MudOverlay Visible="_open"
            @onpointerdown="@(() => CloseMenu(false))"
            LockScroll="@LockScroll"
            AutoClose="@(!Modal)"
            Modal="@Modal"
            OnClosed="@(() => CloseMenu(false))"/>